package com.cc.blog.service.impl;

import com.cc.blog.entity.SysUser;
import com.cc.blog.service.LoginService;
import com.cc.blog.service.TokenService;
import com.cc.blog.vo.ErrorCode;
import com.cc.blog.vo.LoginUserVo;
import com.cc.blog.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class TokenServiceImpl implements TokenService {

    @Autowired
    private LoginService loginService;

    @Override
    public Result getUserInfoByToken(String token) {
        SysUser sysUser =  loginService.checkToken(token);
        if (sysUser == null){
            return Result.fail(ErrorCode.TOKEN_ERROR.getCode(), ErrorCode.TOKEN_ERROR.getMsg())
                    ;        }
        //根据前端给的返回格式封装一个vo类
        LoginUserVo loginUserVo = new LoginUserVo();
        loginUserVo.setId(String.valueOf(sysUser.getId()));
        loginUserVo.setNickname(sysUser.getNickname());
        loginUserVo.setAccount(sysUser.getAccount());
        loginUserVo.setAvatar(sysUser.getAvatar());
        return Result.success(loginUserVo);
    }
}
